home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / mawk10.zip / HASH.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  4KB  |  181 lines

  1.  
  2. /********************************************
  3. hash.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13.  
  14. /* $Log:    hash.c,v $
  15.  * Revision 3.3.1.1  91/09/14  17:23:26  brennan
  16.  * VERSION 1.0
  17.  * 
  18.  * Revision 3.3  91/08/13  06:51:33  brennan
  19.  * VERSION .9994
  20.  * 
  21.  * Revision 3.2  91/06/28  04:16:47  brennan
  22.  * VERSION 0.999
  23.  * 
  24.  * Revision 3.1  91/06/07  10:27:36  brennan
  25.  * VERSION 0.995
  26.  * 
  27.  * Revision 2.2  91/06/04  06:45:10  brennan
  28.  * removed <string.h>
  29.  * 
  30.  * Revision 2.1  91/04/08  08:23:13  brennan
  31.  * VERSION 0.97
  32.  * 
  33. */
  34.  
  35.  
  36. /* hash.c */
  37.  
  38. #include "mawk.h"
  39. #include "memory.h"
  40. #include "symtype.h"
  41.  
  42.  
  43. unsigned hash(s)
  44.   register char *s ;
  45. { register unsigned h = 0 ;
  46.  
  47.   while ( *s )  h += h + *s++ ;
  48.   return  h  ;
  49. }
  50.  
  51. typedef struct hash {
  52. struct hash *link ;
  53. SYMTAB  symtab ;
  54. }  HASHNODE ;
  55.  
  56. static  HASHNODE *PROTO( delete, (char *) ) ;
  57.  
  58. #define  new_HASHNODE() (HASHNODE *) zmalloc(sizeof(HASHNODE))
  59.  
  60. static HASHNODE *hash_table[HASH_PRIME] ;
  61.  
  62. /*
  63.  *   insert -- s is not there and need not be duplicated
  64.  *   -- used during initialization
  65.  */
  66.  
  67. SYMTAB *insert(s) 
  68.   char *s ;
  69. { register HASHNODE *p = new_HASHNODE();
  70.   register unsigned h ;
  71.   
  72.   p->link = hash_table[h = hash(s) % HASH_PRIME ] ;
  73.   p->symtab.name = s ;
  74.   hash_table[h] = p ;
  75.   return &p->symtab ;
  76. }
  77.  
  78. /*
  79.  *  find --  s might be there, find it else insert and dup
  80.  *  s 
  81.  */
  82.  
  83. SYMTAB *find(s)
  84.   char *s ;
  85. { register HASHNODE *p ;
  86.   HASHNODE *q ;
  87.   unsigned h ;
  88.  
  89.   p = hash_table[h = hash(s) % HASH_PRIME ] ;
  90.   q = (HASHNODE *) 0 ;
  91.   while ( 1 )
  92.   { if ( !p )
  93.     { p = new_HASHNODE() ;
  94.       p->symtab.type = ST_NONE ;
  95.       p->symtab.name = strcpy(zmalloc( strlen(s)+1 ), s) ;
  96.       break ;
  97.     }
  98.  
  99.     if ( strcmp(p->symtab.name, s) == 0 ) /* found */
  100.       if ( !q )  /* already at the front */
  101.         return  &p->symtab ;
  102.       else /* delete from the list */
  103.       { q->link = p->link ;  break ; }
  104.  
  105.     q = p ; p = p->link ;
  106.   }
  107.   /* put p on front of the list */
  108.   p->link = hash_table[h] ;
  109.   hash_table[h] = p ;
  110.   return & p->symtab ;
  111. }
  112.  
  113.  
  114. /* remove a node from the hash table
  115.    return a ptr to the node */
  116.  
  117. static unsigned last_hash ;
  118.  
  119. static  HASHNODE  *delete( s )
  120.   char *s ;
  121. { register HASHNODE *p ;
  122.   HASHNODE *q = (HASHNODE *) 0 ;
  123.   unsigned h ;
  124.  
  125.   p = hash_table[ last_hash = h = hash(s) % HASH_PRIME ] ;
  126.   while ( p )
  127.       if ( strcmp(p->symtab.name, s) == 0 )  /* found */
  128.       {
  129.         if ( q )  q->link = p->link ;
  130.         else  hash_table[h] = p->link ;
  131.         return p ;
  132.       }
  133.       else { q = p ; p = p->link ; }
  134.  
  135. #ifdef  DEBUG   /* we should not ever get here */
  136.   bozo("delete") ;
  137. #endif
  138.   return (HASHNODE *) 0 ;
  139. }
  140.  
  141. /* when processing user functions,  global ids which are
  142.    replaced by local ids are saved on this list */
  143.  
  144. static HASHNODE  *save_list ;
  145.  
  146. /* store a global id on the save list,
  147.    return a ptr to the local symtab  */
  148. SYMTAB *save_id( s )
  149.   char *s ;
  150. { HASHNODE *p, *q ;
  151.   unsigned h ;
  152.  
  153.   p = delete(s) ;
  154.   q = new_HASHNODE() ;
  155.   q->symtab.type = ST_LOCAL_NONE ;
  156.   q->symtab.name = p->symtab.name ;
  157.   /* put q in the hash table */
  158.   q->link = hash_table[ h = last_hash ] ;
  159.   hash_table[h] = q ;
  160.  
  161.   /* save p */
  162.   p->link = save_list ; save_list = p ;
  163.  
  164.   return & q->symtab ;
  165. }
  166.  
  167. /* restore all global indentifiers */
  168. void  restore_ids()
  169. { register HASHNODE *p, *q ;
  170.   register unsigned h ;
  171.  
  172.   q = save_list ; save_list = (HASHNODE *) 0 ;
  173.   while ( q )
  174.   {
  175.     p = q ; q = q->link ;
  176.     zfree( delete(p->symtab.name) , sizeof(HASHNODE) ) ;
  177.     p->link = hash_table[h = last_hash ] ; 
  178.     hash_table[h] = p ;
  179.   }
  180. }
  181.